package com.zzptc.sky.myapplication;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.zzptc.sky.myapplication.R;

import java.util.ArrayList;
import java.util.List;

public class YindaoActivity extends AppCompatActivity {

    private ViewPager mViewPager;
    private LinearLayout ll_container;
    private ImageView iv_redPoint;
    private Button btn_start;

    private int[] mImageViewId = new int[]{
            R.drawable.bbb,
            R.drawable.ccc,
            R.drawable.aaa
    };

    private List<ImageView> mImageViewList;

    private int mPaintDis;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //隐藏标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //getSupportActionBar().hide();

        setContentView(R.layout.activity_yindao);

        mViewPager = (ViewPager) findViewById(R.id.viewPager_guide);
        ll_container = (LinearLayout) findViewById(R.id.ll_container);
        iv_redPoint = (ImageView) findViewById(R.id.iv_redPoint);
        btn_start = (Button) findViewById(R.id.btn_start);

        //初始化数据 添加图片和圆点
        initData();

        //ViewPager添加适配器
        GuideAdapter ga = new GuideAdapter();
        mViewPager.setAdapter(ga);

        //监听布局是否完成 布局位置是否确定
        iv_redPoint.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                //避免重复回调 使用过时方法
                iv_redPoint.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                //布局完成 获取第一个小灰点和第二个小灰点之间的left距离
                mPaintDis = ll_container.getChildAt(1).getLeft()-ll_container.getChildAt(0).getLeft();
                Log.d("guide", "onGlobalLayout: "+mPaintDis);
            }
        });

        //viewPage 滑动监听
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            //滑动过程中的回调
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //当滑动到第二个pager的时候，positionOffset的百分比会变成0，position会变成1，所以后面要加上position*mPaintDis
                int leftMargin  = (int) (mPaintDis*positionOffset)+position*mPaintDis;
                //在父容器中设置leftMargin的距离
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) iv_redPoint.getLayoutParams();
                params.leftMargin = leftMargin;
                iv_redPoint.setLayoutParams(params);
            }

            @Override
            public void onPageSelected(int position) {
                Log.d("guide", "onPageSelected: "+position);
                if (position == mImageViewList.size() -1){
                    btn_start.setVisibility(View.VISIBLE);
                }else {
                    btn_start.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.d("guide", "onPageScrollStateChanged: "+state);
            }
        });


        //改变第一次进入真假
        SharedPreferences sp = getSharedPreferences("first_in",MODE_PRIVATE);
        SharedPreferences.Editor e = sp.edit();
        e.putBoolean("isFirstIn",false);
        e.commit();


        //按钮点击事件
        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d("onclick", "onClick: ");
                Intent i = new Intent(YindaoActivity.this,MainActivity.class);
                startActivity(i);
                finish();
            }
        });

    }

    private void initData() {
        mImageViewList = new ArrayList<ImageView>();
        //添加数据
        for (int i = 0; i<mImageViewId.length;i++){

            //添加图片
            ImageView iv = new ImageView(this);
            iv.setBackgroundResource(mImageViewId[i]);
            mImageViewList.add(iv);

            //添加圆点
            ImageView iv_point = new ImageView(this);
            iv_point.setImageResource(R.drawable.gray_point);
            //初始化布局参数
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            if (i>0){
                //当小圆点的个数超过一个 就设置左边距为10
                params.leftMargin = 10;
            }
            iv_point.setLayoutParams(params);
            ll_container.addView(iv_point);
        }
    }

    class GuideAdapter extends PagerAdapter{

        //item个数
        @Override
        public int getCount() {
            return mImageViewList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        //初始化item布局
        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView iv = mImageViewList.get(position);
            container.addView(iv);
            return iv;
        }

        //销毁item
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }
}